package JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SubjectDao {
    public int add(Subject s){
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into s_subject(subjectname,classhour,gradeid) values(?,?,?)";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setString(1,s.getSubjectname());
            st.setInt(2,s.getClasshour());
            st.setLong(3,s.getGradeid());

            return st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, st, connection);
        }
        return  0;
    }
    public int del(long id){
        List<Object> list = new ArrayList<>();
        list.add(id);
        String sql = "Delete from s_subject where id=" + id;
        return DBUtil.executeUpdate(sql, list);
    }
    public int delect(long id){
        return 0;
    }
    public int update(Subject s){
        Connection con=null;
        PreparedStatement pr=null;
        String sql="update s_subject set subjectnam=? where id=?";
        try {
            con = DBUtil.getConnection();
            pr = con.prepareStatement(sql);
            pr.setString(1, s.getSubjectname());
            pr.setLong(2,s.getId());
            return pr.executeUpdate();
//            int rs = pr.executeUpdate();
//            System.out.println(rs==0?"添加失败":"添加成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(pr);
            DBUtil.close(con);
        }
        return 0;
    }

    public List<Subject> search(int pageNo,int pageSize) {
        List<Subject> datas = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
//            con = DBUtil.getConnection();
//            ps = con.prepareStatement(sql);
//            ps.setInt(1,(pageNo-1)*pageSize);
//            ps.setInt(2,pageSize);
//            rs = ps.executeQuery();
            con = DBUtil.getConnection();
            String sql = "select * from s_subject order by id desc limit ?,?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, (pageNo - 1) * pageSize);
            ps.setInt(2, pageSize);
            rs = ps.executeQuery();
            while (rs.next()) {
                datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid")));
            }
        } catch (SQLException e){
            DBUtil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DBUtil.close(rs, ps, con);
        }
        return datas;
    }

    //主键找对象
    public Subject get(long id){
        return null;
    }
    //查询全部对象
    public List<Subject> findALL(){
        return  null;
    }



}
